package com.bosssoft.cloud.wechat.core.router;

import com.bosssoft.cloud.wechat.core.bean.param.portal.WxMsgParam;
import com.bosssoft.cloud.wechat.core.handler.TextHandler;
import com.bosssoft.cloud.wechat.core.infra.config.WxConfig;
import com.bosssoft.cloud.wechat.core.infra.config.WxTemplet;
import com.bosssoft.cloud.wechat.infrastructure.constant.WxKeywordConsts;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 事件router
 * 对接收到的事件请求进行判断和处理
 *
 * @author wcy create on 2021/6/22 11:24 上午
 */

@Component
public class EventRouter {

    @Resource
    private WxConfig wxConfig;
    @Resource
    private TextHandler textHandler;

    /**
     * 根据需求可添加存储用户操作入库等，
     * 便于做粉丝分析等。
     *
     * @param param 用户行为事件param
     * @return 包装后的xml
     */
    public String route(WxMsgParam param) {
        // 判断事件类型
        switch (param.getEvent()) {
            // 关注/取关，调用textHandler返回文本消息
            case WxKeywordConsts.Event.SUBSCRIBE:
            case WxKeywordConsts.Event.UNSUBSCRIBE:
                return textHandler.handle(param,
                        String.format(
                                WxTemplet.event.getData().get(param.getEvent()),
                                wxConfig.getAreaName()
                        ));
            case WxKeywordConsts.Event.VIEW:
            case WxKeywordConsts.Event.TEMPLATESENDJOBFINISH:
                break;
        }
        return WxKeywordConsts.SUCCESS;
    }
}
